const { db, query } = require("../database/db")

// 活动列表
exports.activesList = async (page, pageSize, name, location) => {
    let listSql = `
        SELECT actives.name,actives.location,actives.detail,actives.latitude,actives.longitude,actives.talk,actives.time,pic,actives.publisher_id,actives.publisher,actives.views,users.username
        FROM actives 
        JOIN users ON users.id = actives.publisher_id
    `
    // 初始化参数数组
    let params = [];

    // 如果有搜索条件，添加到SQL语句中
    let conditions = [`actives.isDeleted = 0`]; // 默认条件，用户状态为0
    if (name) {
        conditions.push(`actives.name LIKE ?`);
        params.push(`%${name}%`);
    }
    if (location) {
        conditions.push(`actives.location LIKE ?`);
        params.push(`%${location}%`);
    }
    // 如果有搜索条件，添加WHERE子句
    if (conditions.length > 0) {
        listSql += ` WHERE ${conditions.join(' AND ')}`;
    }
    // 添加分页的SQL语句
    listSql += ` LIMIT ? , ?`;
    // (page - 1) * pageSize, page * pageSize
    // 添加分页参数
    params.push((page - 1) * pageSize);
    params.push(pageSize);
    // 计算总数的SQL语句
    let countSql = `SELECT COUNT(*) as count FROM actives`;
    if (conditions.length > 0) {
        countSql += ` WHERE ${conditions.map(c => `(${c})`).join(' AND ')}`;
    }
    // 执行查询
    const [usersResult, countResult] = await Promise.all([
        query(listSql, params), // 查询用户列表
        query(countSql, params) // 查询总数
    ]);
    console.log("结果：：：", usersResult)
    return {
        users: usersResult, // 用户列表
        total: countResult // 总数
        // total: countResult.total[0].count
    };
}

// 添加活动
exports.addactive = async (name, location, detail, latitude, longitude, talk, time, now_dateTime, pic, publisher_id, publisher) => {
    let sql = `
        INSERT INTO actives (name, location, detail, latitude, longitude, talk, time, createTime, pic, publisher_id, publisher)
        VALUES (?,?,?,?,?,?,?,?,?,?,?)
    `
    return query(sql, [name, location, detail, latitude, longitude, talk, time, now_dateTime, pic, publisher_id, publisher])
}

// 删除活动---更新状态
exports.Fdelete = async (id, now_dateTime) => {
    let sql_active = `
        UPDATE actives SET isDeleted = 1,deletedAt = ?
        WHERE id = ?
    `
    let sql_sign = `
        UPDATE active_users SET isDeleted = 'true' 
        WHERE active_id = ?
    `
    const [activeResult,sginResult] = await Promise.all([
        query(sql_active,[id, now_dateTime]),
        query(sql_sign,[id])
    ])
    return {
        activeResult: activeResult, // 活动列表
        sginResult: sginResult // 报名列表
    };
}

// 删除活动---删除记录
exports.Tdelete = async (id) => {
    let sql_active = `
        DELETE FROM actives
        WHERE id = ?
    `
    let sql_sign = `
        DELETE FROM active_users
        WHERE active_id = ?
    `
    const [activeResult,sginResult] = await Promise.all([
        query(sql_active,[id]),
        query(sql_sign,[id])
    ])
    return {
        activeResult: activeResult, // 活动列表
        sginResult: sginResult // 报名列表
    };
}

// 获取记录
exports.get = async (id) => {
    let sql = `
        SELECT actives.name,actives.location,actives.detail,actives.latitude,actives.longitude,actives.talk,actives.time,pic,
        actives.publisher_id,actives.publisher,actives.views,users.username
        FROM actives
        JOIN users ON users.id = actives.publisher_id
        WHERE actives.id = ?
    `
    return query(sql, [id])
}

// 更新数据
exports.set = async (id, name, location, detail, time, latitude, longitude, talk, pic, publisher) => {
    let sql = `
        UPDATE actives SET 
            name=?, location=?, detail=?, time=?, latitude=?, longitude=?, talk=?, pic=?, publisher=? 
        WHERE id = ?
    `

    return query(sql, [name, location, detail, time, latitude, longitude, talk, pic, publisher, id])
}